using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("CodeStory.WebControls.Resource.RichHtmlTextBox.js", "application/x-javascript")]
namespace CodeStory.WebControls.RichHtmlTextBox
{
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:RichHtmlTextBox runat=server></{0}:RichHtmlTextBox>")]
	public class RichHtmlTextBox : WebControl, INamingContainer,IPostBackDataHandler
	{
		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string HtmlText {
			get {
				String s = (String)ViewState["HtmlText"];
				return ((s == null) ? String.Empty : s);
			}

			set { ViewState["HtmlText"] = value; }
		}

		[DefaultValue(TextBoxView.Preview)]
		public TextBoxView TextBoxView {
			get {
				object obj = ViewState["TextBoxView"];
				return obj == null ? TextBoxView.Preview : (TextBoxView)obj;
			}
			set { ViewState["TextBoxView"] = value; }
		}

		protected override HtmlTextWriterTag TagKey {
			get { return HtmlTextWriterTag.Div; }
		}

		protected override void CreateChildControls() {
			Controls.Clear();

			Table table = new Table();
			Controls.Add(table);
			table.ID = "Table";
			table.CellPadding = 0;
			table.CellSpacing = 0;
			table.Width = Width;
			table.Height = Height;
			TableRow row;
			row = new TableRow();
			table.Rows.Add(row);
			row.Height = Unit.Percentage(100);
			TableCell cell;
			cell = new TableCell();
			row.Cells.Add(cell);
			Panel panel = new Panel();
			cell.Controls.Add(panel);
			panel.ID = "Editor";
			TextBoxID = panel.ClientID;
			panel.BorderStyle = this.BorderStyle;
			panel.BorderWidth = this.BorderWidth;
			panel.BorderColor = this.BorderColor;
			panel.Width = Unit.Percentage(100);
			panel.Height = Unit.Percentage(100);
			panel.Attributes.Add("contenteditable","true");
			panel.Attributes.Add("onblur", "javascript:RichHtmlTextBox.Blur();");
			panel.Style.Add(HtmlTextWriterStyle.Overflow,"auto");

			row = new TableRow();
			table.Controls.Add(row);
			row.Height = Unit.Pixel(25);
			cell = new TableCell();
			row.Cells.Add(cell);
			LinkButton lnk;
			lnk = new LinkButton();
			cell.Controls.Add(lnk);
			lnk.ID = "Source";
			lnk.Text = SourceButtonText;
			lnk.Attributes.Add("onclick", "javascript:return RichHtmlTextBox.ShowSource();");
			lnk = new LinkButton();
			cell.Controls.Add(lnk);
			lnk.ID = "Preview";
			lnk.Text = PreviewButtonText;
			lnk.Attributes.Add("onclick", "javascript:return RichHtmlTextBox.ShowPreview();");
			

			ChildControlsCreated = true;
		}

		[DefaultValue("Preview")]
		private string PreviewButtonText {
			get {
				object obj = ViewState["PreviewButtonText"];
				return obj == null ? "Preview" : (string)obj;
			}
			set { ViewState["PreviewButtonText"] = value; }
		}
		[DefaultValue("Source")]
		private string SourceButtonText {
			get {
				object obj = ViewState["SourceButtonText"];
				return obj == null ? "Source" : (string)obj;
			}
			set { ViewState["SourceButtonText"] = value; }
		}

		string _textBoxID = "";
		private string TextBoxID {
			get { return _textBoxID; }
			set { _textBoxID = value; }
		}

		protected override void OnPreRender(EventArgs e) {
			Page.ClientScript.RegisterClientScriptResource(typeof(DataSourceHelper), "CodeStory.WebControls.Resource.CodeStory.js");
			Page.ClientScript.RegisterClientScriptResource(GetType(), "CodeStory.WebControls.Resource.RichHtmlTextBox.js");
			Page.ClientScript.RegisterHiddenField(this.ClientID,HtmlText);
			Page.ClientScript.RegisterHiddenField(this.ClientID + "_TextBoxView","");

			string script = @"
RichHtmlTextBox.TextBoxID = """ + this.TextBoxID + @""";
RichHtmlTextBox.ValueFieldID = """ + this.ClientID + @""";
RichHtmlTextBox.TextBoxViewID = """ + this.ClientID + @"_TextBoxView"";
";
			switch (TextBoxView) {
				case TextBoxView.Source:
					script += @"
window.attachEvent(""onload"",function(){javascript:return RichHtmlTextBox.ShowSource();});
";
					break;
				default: 
					script += @"
window.attachEvent(""onload"",function(){javascript:return RichHtmlTextBox.ShowPreview();});
";
					break;
			}
			Page.ClientScript.RegisterClientScriptBlock(GetType(),"startup",script,true);

			Page.RegisterRequiresPostBack(this);
			base.OnPreRender(e);
		}

		public bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
			string data = postCollection[postDataKey];
			if (!HtmlText.Equals(data)) {
				HtmlText = data;
				return true;
			}
			string view = postCollection[ClientID + "_TextBoxView"];
			if (view != null) {
				if (view == "SOURCE") {
					TextBoxView = TextBoxView.Source;
				} else {
					TextBoxView = TextBoxView.Preview;
				}
			}
			return false;
		}

		private static object TextChangedEventObject = new object();
		public event EventHandler TextChanged {
			add { Events.AddHandler(TextChangedEventObject,value); }
			remove { Events.RemoveHandler(TextChangedEventObject, value); }
		}

		protected void OnTextChanged(EventArgs e) {
			EventHandler handler = (EventHandler)Events[TextChangedEventObject];
			if (handler != null) {
				handler(this,e);
			}
		}


		public void RaisePostDataChangedEvent() {
			this.OnTextChanged(EventArgs.Empty);
		}

		protected override void Render(HtmlTextWriter writer) {
			RenderContents(writer);
		}
	}

	public enum TextBoxView : byte
	{
		Source = 0,
		Preview
	}
}